from __future__ import absolute_import
from forest.registry import register_tool

from forest.tools.system.execute import run

@register_tool('udev.settle')
def udev_settle(): #timeout = 300):
    ''' Waits for udev to "settle".

        This is so blkid, lvm, mdadm can have a chance to understand devices after modifying them.

        timeout - default: 300 - needs to be long for machine with several and/or slow disks

    '''
    run(["udevadm", "settle", "--timeout=300"])


#def udev_trigger(subsystem=None, action="add"):
#    argv = ["trigger", "--action=%s" % action]
#    if subsystem:
#        argv.append("--subsystem-match=%s" % subsystem)
#
#    iutil.execWithRedirect("udevadm", argv, stderr="/dev/null")
#
#def udev_parse_uevent_file(dev):
#    path = os.path.normpath("/sys/%s/uevent" % dev['sysfs_path'])
#    if not os.access(path, os.R_OK):
#        return dev
#
#    with open(path) as f:
#        for line in f.readlines():
#            (key, equals, value) = line.strip().partition("=")
#            if not equals:
#                continue
#
#            dev[key] = value
#
#    return dev
